package com.htc.callcare.viewcontroller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.htc.callcare.R;
import com.htc.callcare.activities.RecordActivity;
import com.htc.callcare.activities.VoiceMailActivity;
import com.htc.callcare.infrastructure.globalVar;

public class Player extends LinearLayout {

	MediaPlayer mediaPlayer = null;
	private boolean isPaused = false;
	private boolean isStop = true;
	private boolean isPlay = false;

	String filePath = null;
	String productName = null;

	Button btnPlay = null;
	Button btnStop = null;
	Button btnClose = null;
	String filename;

	AudioManager audioManager = null;
	SeekBar progressControl = null;
	TextView progressText = null;
	long durationInMillis = -1;
	Context context;

	public Player(Context context) {
		super(context);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.player, this, true);
		progressText = (TextView) findViewById(R.id.playerProcessText);
		progressControl = (SeekBar) findViewById(R.id.playerSeekbar);
		btnPlay = (Button) findViewById(R.id.playerPlay);
		btnStop = (Button) findViewById(R.id.playerStop);
		btnClose = (Button) findViewById(R.id.playerClose);
		initListener();
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	private void initListener() {
		// TODO Auto-generated method stub
		btnPlay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isStop && !isPlay && !isPaused) {
					release();
					setPlayer();
					isStop = false;
					btnPlay.setText("PlayAfterStop");
					onEverySecond.run();
				}
				if (!isPaused) {
					mediaPlayer.pause();
					btnPlay.setText("PauseWhilePlay");
					isPaused = true;
					
				} else if (isPlay) {
					mediaPlayer.start();
					btnPlay.setText("StartAfterPause");
					isPaused = false;
					isPlay = true;
					onEverySecond.run();
				}
			}
		});

		btnStop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setStop();

			}
		});

		btnClose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setStop();
				Player.this.setVisibility(View.GONE);
			}
		});
		if (mediaPlayer != null) {
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					isPlay = false;
					isStop = true;
					isPlay = false;
				}
			});
		}

	}

	public void setStop() {
		if (mediaPlayer != null) {
			isStop = true;
			isPaused = false;
			isPlay = false;
			progressControl.setProgress(0);
			mediaPlayer.stop();
			btnPlay.setText("Play");
		}
	}

	public void setPlayer() {
		try {
			isPlay = true;
			if (mediaPlayer != null) {
				mediaPlayer.stop();
			}
			String path ="";
			if(context instanceof RecordActivity){
				path = globalVar.recordDir + filename;
			}else if(context instanceof VoiceMailActivity){
				path = globalVar.voiceMailDir + filename;
			}
			mediaPlayer = new MediaPlayer();
			
			File file = new File(path);
			FileInputStream fis = new FileInputStream(file);
			//Set maximum volumn
			mediaPlayer.setVolume(1.0f, 1.0f);
			mediaPlayer.setDataSource(fis.getFD());
			mediaPlayer.prepare();
			mediaPlayer.start();
			
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			progressText.postDelayed(onEverySecond, 1000);
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					btnPlay.setText("Play");
				}
			});
			Player.this.setProgressControl();

		} catch (IllegalArgumentException e) {
			isPlay = false;
			e.printStackTrace();
		} catch (IllegalStateException e) {
			isPlay = false;
			e.printStackTrace();
		} catch (IOException e) {
			isPlay = false;
			Toast.makeText(getContext(), "select one", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}

	private void setProgressControl() {
		int maxVolume = mediaPlayer.getDuration();
		int curVolume = mediaPlayer.getCurrentPosition();

		progressControl.setMax(maxVolume);
		progressControl.setProgress(curVolume);
		progressControl
				.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					@Override
					public void onProgressChanged(SeekBar seekbar,
							int progress, boolean fromUser) {
						mediaPlayer.seekTo(progress);

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}
				});
	}

	protected void setProgressText() {
		durationInMillis = mediaPlayer.getDuration();
		int curVolume = mediaPlayer.getCurrentPosition();
		long HOUR = 60 * 60 * 1000;
		if (progressText != null) {
			if (durationInMillis > HOUR) {
				progressText.setText(String.format("%1$tH:%1$tM:%1$tS",
						new Date(curVolume))
						+ " / "
						+ String.format("%1$tH:%1$tM:%1$tS", new Date(
								durationInMillis)));
			} else {
				progressText.setText(String.format("%1$tM:%1$tS", new Date(
						curVolume))
						+ " / "
						+ String.format("%1$tM:%1$tS", new Date(
								durationInMillis)));
			}
		}
	}

	private Runnable onEverySecond = new Runnable() {
		public void run() {

			if (mediaPlayer != null) {
				progressControl.setProgress(mediaPlayer.getCurrentPosition());

				Player.this.setProgressText();
			}

			if (!isPaused) {
				progressText.postDelayed(onEverySecond, 1000);
			}
		}
	};

	public void release() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	public void stop() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
		}
	}
	
	public void hidePlayer(){
		setStop();
		Player.this.setVisibility(View.GONE);
	}
}
